from flask import Flask, render_template, request, send_file, jsonify
import pandas as pd
import numpy as np
import os
import sys
import webbrowser
from threading import Timer
import glob
from functools import lru_cache
import hashlib
import json
# from flight_expense_route import flight_expense_bp  # 导入飞机费用分析路由
from personnel_network_route import personnel_network_bp  # 导入人员关系网络分析路由
from information_assets_route import information_assets_bp
from specific_personnel_exchange_route import specific_personnel_exchange_bp
from transaction_type_route import bank_flow_bak
from abnormal_warning_route import abnormal_warning  # 导入异常预警路由
from bank_flow_route import bank_flow_bp
from asset_situation_analysis_router import asset_situation_bp
from three_bussiness_route import three_bussiness_bp

# 添加一个全局变量来跟踪浏览器是否已经打开
browser_opened = False

def resource_path(relative_path):
    """获取资源的绝对路径"""
    try:
        # PyInstaller创建临时文件夹,将路径存储在_MEIPASS中
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")
    return os.path.join(base_path, relative_path)

app = Flask(__name__, 
            template_folder=resource_path('templates'))

# 注册蓝图
# app.register_blueprint(flight_expense_bp)  # 注册飞机费用分析蓝图
app.register_blueprint(personnel_network_bp)  # 注册人员关系网络分析蓝图
app.register_blueprint(information_assets_bp, url_prefix='/information-assets')
app.register_blueprint(bank_flow_bak, url_prefix='/bank-flow-bak')
app.register_blueprint(bank_flow_bp, url_prefix='/bank-flow')
app.register_blueprint(specific_personnel_exchange_bp, url_prefix='/specific-personnel-exchange')
app.register_blueprint(asset_situation_bp, url_prefix='/asset-situation')
app.register_blueprint(abnormal_warning)  # 注册异常预警蓝图
app.register_blueprint(three_bussiness_bp, url_prefix='/three-bussiness')

# 配置上传文件存储路径和数据文件夹路径
if getattr(sys, 'frozen', False):
    # 如果是打包后的exe
    UPLOAD_FOLDER = os.path.join(os.path.dirname(sys.executable), 'uploads')
    DATA_FOLDER = os.path.join(os.path.dirname(sys.executable), 'data')
else:
    # 如果是开发环境
    UPLOAD_FOLDER = 'uploads'
    DATA_FOLDER = 'data'

# 确保必要的文件夹存在
for folder in [UPLOAD_FOLDER, DATA_FOLDER]:
    if not os.path.exists(folder):
        os.makedirs(folder)

@app.route('/')
def index():
    return render_template('pages/personnel_network.html', active_page='personnel_network')

@app.route('/three-bussiness')
def three_bussiness():
    return render_template('pages/three_bussiness.html', active_page='three_bussiness')

@app.route('/personnel-network')
def personnel_network():
    return render_template('pages/personnel_network.html', active_page='personnel_network')

@app.route('/information-assets')
def information_assets():
    return render_template('pages/information_assets.html', active_page='information_assets')

@app.route('/bank-flow-bak')
def bank_flow_bak():
    return render_template('pages/bank_flow_bak.html', active_page='bank_flow_bak')

@app.route('/specific-personnel-exchange')
def specific_personnel_exchange():
    return render_template('pages/specific_personnel_exchange.html', active_page='specific_personnel_exchange')

@app.route('/bank-flow')
def bank_flow():
    return render_template('pages/bank_flow.html', active_page='bank_flow')

@app.route('/asset-situation')
def asset_situation():
    return render_template('pages/asset_situation.html', active_page='asset_situation')

@app.route('/Asset-BI-Chart')
def Asset_BI_Chart():
    return render_template('pages/Asset_BI_Chart.html', active_page='Asset_BI_Chart')

@app.route('/abnormal-warning')
def abnormal_warning():
    return render_template('pages/abnormal_warning.html', active_page='abnormal_warning')

def open_browser():
    """打开浏览器，确保只打开一次"""
    global browser_opened
    if not browser_opened:
        webbrowser.open('http://127.0.0.1:5001/')
        browser_opened = True

if __name__ == '__main__':
    # 使用更长的延时，确保服务器已经完全启动
    Timer(2, open_browser).start()
    app.run(debug=False, port=5001) 